package com.kaka.bus.engine.borrow.signed;

import com.kaka.bus.bean.HttpWrapper;
import com.kaka.bus.di.DataManager;
import com.kaka.bus.engine.RouterUtil;
import com.kaka.bus.manager.Result;
import com.kaka.bus.manager.T;
import com.kaka.bus.mvp.RxPresenter;
import com.kaka.bus.util.U;

import javax.inject.Inject;

/**
 * Created by zhangqi on 2016/10/21.
 */

public class SignedPresenter extends RxPresenter<SignedContract.View> implements SignedContract.Presenter {

    @Inject
    public SignedPresenter(DataManager dataManager) {
        super(dataManager);
    }

    @Override
    public void getSignedResult(String userId, String token,String nodeId) {
        mView.showDialog("查询结果中...这可能会等待几分钟");

        addSubscribe(mDataManager
                .getApi()
                .getNodeStatus(userId,nodeId,token, U.Sign(userId,nodeId,token))
                .compose(T.D())
                .subscribeWith(new Result<HttpWrapper<String>>() {
                    @Override
                    protected void onSuccess(HttpWrapper<String> nodeHttpWrapper) {
                        mView.validate(nodeHttpWrapper);
                    }

                    @Override
                    protected void onFinish() {
                        mView.dissDialog();
                    }
                }));
    }

    @Override
    public void router(SignedActivity signedActivity) {
        mView.showDialog("查询节点中...");
        RouterUtil.router(signedActivity, mDataManager.getApi(),mCompositeDisposable, () -> {
            if(mView != null){
                mView.dissDialog();
            }
        });
    }

    @Override
    public void NodeOk(String userId, String token) {
        mView.showDialog("稍等一会...");
        addSubscribe(mDataManager
                .getApi()
                .NodeOk(userId,token,U.Sign(userId,token))
                .compose(T.D())
                .subscribeWith(new Result<HttpWrapper<String>>() {
                    @Override
                    protected void onSuccess(HttpWrapper<String> wrapper) {
                        mView.NodeOk(wrapper);
                    }

                    @Override
                    protected void onFinish() {
                        super.onFinish();
                        mView.dissDialog();
                    }
                }));
    }
}
